<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\api\controller;
use app\service\Rsa;

class Loading extends Base{
    /**
     * 闪屏页获取公钥
     * @return \think\response\Json
     */
    public function sendKey(){
        if(request()->isPost()){
            $post = input('param.');
            $rsa  = new Rsa();
            try{
                // 验证参数
                if(!isset($post['equipment'])){
                    throw new \LogicException('参数错误',1000);
                }
                $publicKey = $rsa->generateKey($post['equipment']);
            } catch (\LogicException $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult(['public_key'=>$publicKey]));
        }
    }

    /**
     * 检查token是否失效
     * @return \think\response\Json
     */
    public function checkToken(){
        if(request()->isPost()) {
            try{
                // 登录验证
                self::checkLogin();
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }

    /**
     * 加密测试
     */
    public function encrypt(){
        $post = input('param.');
        $rsa = new Rsa();
        $key = $rsa->generateKey($post['equipment']);
        unset($post['equipment']);
        $result['encrypt'] = $rsa->encrypt($key,$post);
        return json(self::formatSuccessResult($result));
    }
}